package com.cn.xiaonuo.tcrfinance.tcragents.service.impl;


import cn.hutool.core.convert.Convert;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.tcrfinance.tcragents.entiy.*;
import com.cn.xiaonuo.tcrfinance.tcragents.service.AgentFinanceService;
import com.cn.xiaonuo.tcrfinance.tcragents.utils.AgentCst;
import com.cn.xiaonuo.tcrfinance.tcragents.utils.AuthenticationIdentity;
import com.cn.xiaonuo.tcrfinance.tcragents.utils.GzAgentCst;
import com.cn.xiaonuo.tcrfinance.tcragents.utils.TimeFmt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AgentFinanceServiceImpl implements AgentFinanceService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Environment env;

    @Override
    public BusinessMessage queryRoyaltyTol(Map<String,Object> map) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") + AgentCst.queryRoyaltyTol, map, Map.class);
        message.setCode( Convert.toStr(data.get("code")));
        message.setSuccess( Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        return message;
    }

    @Override
    public BusinessMessage queryRoyaltyShop(Map<String, Object> map) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        String passKey = LocalDateTime.now().format(TimeFmt.dtf);
        String realKey = AuthenticationIdentity.doMachiningPassKey(passKey);
        map.put("passKey",passKey);
        map.put("realKey",realKey);
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent")+AgentCst.queryRoyaltyShop, map, Map.class);
        message.setCode( Convert.toStr(data.get("code")));
        message.setSuccess( Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        return message;
    }

    @Override
    public BusinessMessage queryUserDetails(Map<String, Object> map) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        String passKey = LocalDateTime.now().format(TimeFmt.dtf);
        String realKey = AuthenticationIdentity.doMachiningPassKey(passKey);
        map.put("passKey",passKey);
        map.put("realKey",realKey);
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") + AgentCst.queryUserDetails, map, Map.class);
        message.setCode( Convert.toStr(data.get("code")));
        message.setSuccess( Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        return message;
    }

    @Override
    public BusinessMessage queryRoyDetails(Map<String, Object> map) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        String passKey = LocalDateTime.now().format(TimeFmt.dtf);
        String realKey = AuthenticationIdentity.doMachiningPassKey(passKey);
        map.put("passKey",passKey);
        map.put("realKey",realKey);
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") + AgentCst.queryRoyDetails, map, Map.class);
        message.setCode( Convert.toStr(data.get("code")));
        message.setSuccess( Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        return message;
    }

    @Override
    public BusinessMessage queryShopDetails() {
        BusinessMessage<Object> message = new BusinessMessage<>();
        String passKey = LocalDateTime.now().format(TimeFmt.dtf);
        String realKey = AuthenticationIdentity.doMachiningPassKey(passKey);
        Map<String, Object> map = new HashMap<>();
        map.put("passKey",passKey);
        map.put("realKey",realKey);
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") + AgentCst.queryShopDetails, map, Map.class);
        message.setCode( Convert.toStr(data.get("code")));
        message.setSuccess( Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        return message;
    }

    @Override
    public BusinessMessage financialTolRoy(SlXsAgentRoyalty sr) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
                AgentCst.financialTolRoy, sr, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        return message;
    }


    @Override
    public BusinessMessage financialRoyDetails(SlXsAgentRoyalty sr) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
                AgentCst.financialRoyDetails, sr, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        message.setPageSize(Convert.toInt(data.get("pageSize")));
        message.setPageNum(Convert.toInt(data.get("pageNum")));
        message.setDataTol(Convert.toInt(data.get("dataTol")));
        return message;
    }

    @Override
    public BusinessMessage financialSureRoy(List<SlXsAgentRoyalty> royList) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
                GzAgentCst.financialSureRoy, royList, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        return message;
    }

    @Override
    public BusinessMessage financialDetailsRoy(SlXsAgentRoyalty roy) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
                GzAgentCst.financialDetailsRoy, roy, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        return message;
    }


    @Override
    public BusinessMessage financialCycles(AppsCyc appsCyc) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
                AgentCst.financialCycles, appsCyc, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        message.setPageSize(Convert.toInt(data.get("pageSize")));
        message.setPageNum(Convert.toInt(data.get("pageNum")));
        message.setDataTol(Convert.toInt(data.get("dataTol")));
        return message;
    }

    @Override
    public BusinessMessage financialOpCycTrade(SlShop appsCyc) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
            AgentCst.financialOpCycTrade, appsCyc, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        message.setPageSize(Convert.toInt(data.get("pageSize")));
        message.setPageNum(Convert.toInt(data.get("pageNum")));
        message.setDataTol(Convert.toInt(data.get("dataTol")));
        return message;
    }

    @Override
    public BusinessMessage financialSpRoyTolGb(AppsCyc appsCyc) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
                AgentCst.financialSpRoyTolGb, appsCyc, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        message.setPageSize(Convert.toInt(data.get("pageSize")));
        message.setPageNum(Convert.toInt(data.get("pageNum")));
        message.setDataTol(Convert.toInt(data.get("dataTol")));
        return message;
    }


    @Override
    public BusinessMessage financialAgentTotalInfo(CycleInfo cycInfo) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
                AgentCst.financialAgentTotalInfo, cycInfo, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        return message;
    }

    @Override
    public BusinessMessage financialHasRoyAgentInfo(RoyCycInfo cInfo) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
                AgentCst.financialHasRoyAgentInfo, cInfo, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        message.setPageSize(Convert.toInt(data.get("pageSize")));
        message.setPageNum(Convert.toInt(data.get("pageNum")));
        message.setDataTol(Convert.toInt(data.get("dataTol")));
        return message;
    }

    @Override
    public BusinessMessage financialCycRoyDetails(RoyDetailInfo cInfo) {
        BusinessMessage<Object> message = new BusinessMessage<>();
        Map data = restTemplate.postForObject(env.getProperty("https.jybAgent") +
                AgentCst.financialCycRoyDetails, cInfo, Map.class);
        message.setCode(Convert.toStr(data.get("code")));
        message.setSuccess(Convert.toBool(data.get("success")));
        message.setData(data.get("data"));
        message.setMessage(Convert.toStr(data.get("msg")));
        message.setPageSize(Convert.toInt(data.get("pageSize")));
        message.setPageNum(Convert.toInt(data.get("pageNum")));
        message.setDataTol(Convert.toInt(data.get("dataTol")));
        return message;
    }


}
